package com.lottery.ruok.lottrey.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/10/8$
 * <p>
 * 描述：
 */
public class InfoAnnouncementDetailActivity extends BaseActivity {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.divider)
    View divider;
    @BindView(R.id.wv_webview)
    WebView wv_webview;
    private String titleData;
    private String create_time;
    private String content;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_announcement_detail);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        if (getIntent() != null) {
            titleData = getIntent().getStringExtra("title");
            create_time = getIntent().getStringExtra("create_time");
            content = getIntent().getStringExtra("content");
            title.setText(titleData);
            time.setText(create_time);
            wv_webview.setWebViewClient(new WebViewClient(){
                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    handler.proceed();
                }
            });
            wv_webview.getSettings().setJavaScriptEnabled(true);
            String htmlData1=replaceImgStyle(content);
            String imgStyle = "<style> img{ max-width:100%; height:auto;} a{color:#666666;text-decoration:none;background:#fff;}a:active{background:#fff;} a:focus{background:#fff;} a:hover{ background:#fff; color:#666666} </style>";
            htmlData1=imgStyle+htmlData1;
           wv_webview.loadData(htmlData1, "text/html; charset=UTF-8", null);

        }

    }
    /**
     * 使用正则表达式 把html标签中的style属性全部替换成""
     */
    private String replaceImgStyle(String html) {
        String reg = "style=\"([^\"]+)\"";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(html);
        return matcher.replaceAll("");
    }
}
